﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_client.csharp.model.vo;
using mars_client.gui.page;
using mars_client.csharp.util;
using mars_client.csharp.vo;
using mars_client.csharp.cache;
using System.Windows;
using System.Windows.Controls;
using mars_client.gui.window;
using mars_client.csharp.model;
using mars_client.csharp.enums;
using mars_client.csharp.radar;

namespace mars_client.csharp.ao.impl
{
    class ChatAOImpl : ChatAO
    {
        private ChatAOImpl() { }
        private readonly static ChatAOImpl instance = new ChatAOImpl();
        internal static ChatAOImpl Instance
        {
            get
            {
                return instance;
            }
        }
        public void setSingleChatContent(SingleChatVO singleChatVO)
        {
            GuiCache guic = GuiCache.Instance;
            SingleChatPage scp =  (SingleChatPage)guic.getGui(singleChatVO.senderVO.id + "_SingleChatPage");
            //MessageBox.Show("当前搜索的page为" + singleChatVO.senderVO.id + "_SingleChatPage");
            if(scp == null)
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    scp = new SingleChatPage();
                    guic.setGui(singleChatVO.senderVO.id + "_SingleChatPage", scp);
                    //MessageBox.Show("来自 " + singleChatVO.senderVO.nickName, "有新消息啦！！", MessageBoxButton.OK, MessageBoxImage.Information);
                });
            }else
            {
                if(guic.getRightContentUid() != singleChatVO.senderVO.id.ToString())
                {
                    //MessageBox.Show("来自 " + singleChatVO.senderVO.nickName, "有新消息啦！！", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            ChatListPage clp = (ChatListPage)guic.getGui("ChatListPage");
            clp.Dispatcher.Invoke(() =>
            {
                foreach (Grid g in clp.listWrapper.Children)
                {
                    if(g.Uid == singleChatVO.senderVO.id.ToString())
                    {
                        int count = Int32.Parse(((TextBox)g.Children[7]).Text) + 1;
                        //MessageBox.Show(count.ToString());
                        ((TextBox)g.Children[7]).Text = count.ToString();
                        ((TextBox)g.Children[7]).Visibility = Visibility.Visible;
                    }
                }
            });
            scp.Dispatcher.Invoke(() =>
            {
                scp.scroller.ScrollToBottom();
                scp.Uid = singleChatVO.senderVO.id.ToString();
                scp.remoteName.Text = singleChatVO.senderVO.nickName;
                scp.intro.Text = singleChatVO.senderVO.userIntro;
                scp.headImg.Source = SysUtil.netImageSource(singleChatVO.senderVO.headImg);
                //MessageBox.Show("当前用户ID:" + UserCache.Instance.userVO.id + " 信息发送者ID:" + singleChatVO.senderVO.id);
                LogUtil.console("当前用户ID:" + UserCache.Instance.userVO.id + " 信息发送者ID:" + singleChatVO.senderVO.id);
                if (UserCache.Instance.userVO.id != singleChatVO.senderVO.id)
                {
                    scp.contentWrapper.Children.Add(RenderUtil.generateChatTemp(singleChatVO, "left"));
                }else
                {
                    // 判断是否发送失败
                    if(singleChatVO.messageVO == null)
                    {
                        MessageBox.Show("对方不在线，发送失败");
                        LogUtil.console("对方不在线，发送失败");
                    }
                }
            });
        }

        public void setGroupContent(GroupChatVO groupChatVO)
        {
            GuiCache guic = GuiCache.Instance;
            GroupChatPage scp = (GroupChatPage)guic.getGui(groupChatVO.groupVO.id + "_GroupChatPage");
            //MessageBox.Show("当前搜索的page为" + groupChatVO.groupVO.id + "_GroupChatPage");
            if (scp == null)
            {
                App.Current.Dispatcher.Invoke(() =>
                {
                    scp = new GroupChatPage();
                    guic.setGui(groupChatVO.groupVO.id + "_GroupChatPage", scp);
                    
                });
            }
            else
            {
                if (guic.getRightContentUid() != groupChatVO.senderVO.id.ToString())
                {
                    
                }
            }
            ChatListPage clp = (ChatListPage)guic.getGui("ChatListPage");
            clp.Dispatcher.Invoke(() =>
            {
                foreach (Grid g in clp.listWrapper.Children)
                {
                    if (g.Uid == groupChatVO.groupVO.id.ToString())
                    {
                        int count = Int32.Parse(((TextBox)g.Children[7]).Text) + 1;
                        //MessageBox.Show(count.ToString());
                        ((TextBox)g.Children[7]).Text = count.ToString();
                        ((TextBox)g.Children[7]).Visibility = Visibility.Visible;
                    }
                }
            });
            scp.Dispatcher.Invoke(() =>
            {
                scp.scroller.ScrollToBottom();
                scp.Uid = groupChatVO.groupVO.id.ToString();
                scp.remoteName.Text = groupChatVO.groupVO.groupName;
                scp.intro.Text = groupChatVO.groupVO.groupIntro;
                scp.headImg.Source = SysUtil.netImageSource("http://pic.qiantucdn.com/58pic/13/81/87/16F58PICUjV_1024.png");
                //MessageBox.Show("当前用户ID:"+UserCache.Instance.userVO.id + " 信息发送者ID:"+ groupChatVO.senderVO.id);
                LogUtil.console("当前用户ID:" + UserCache.Instance.userVO.id + " 信息发送者ID:" + groupChatVO.senderVO.id);
                if (UserCache.Instance.userVO.id != groupChatVO.senderVO.id)
                {
                    SingleChatVO singleChatVO = new SingleChatVO();
                    singleChatVO.senderVO = groupChatVO.senderVO;
                    singleChatVO.sendTime = groupChatVO.sendTime;
                    singleChatVO.messageVO = groupChatVO.messageVO;
                    scp.contentWrapper.Children.Add(RenderUtil.generateChatTemp(singleChatVO, "left"));
                    scp.scroller.ScrollToBottom();
                }
                else
                {
                    // 判断是否发送失败
                    if (groupChatVO.messageVO == null)
                    {
                        MessageBox.Show("你已不是对方好友，发送失败");
                        LogUtil.console("你已不是对方好友，发送失败");
                    }
                }
            });
        }

        public void createGroup(GroupVO groupVO)
        {
            GuiCache guic = GuiCache.Instance;
            CreateGroupWindow createGroupWindow = (CreateGroupWindow)guic.getGui("CreateGroupWindow");
            if(createGroupWindow != null)
            {
                if(groupVO != null)
                {
                    createGroupWindow.Dispatcher.Invoke(() =>
                    {
                        createGroupWindow.Close();
                        guic.removeGui("CreateGroupWindow");
                        MessageCO messageCO = new MessageCO();
                        messageCO.type = (int)RequestEnum.request.GetGroupList;
                        Radar.Instance.sendData(messageCO);
                        LogUtil.console(groupVO.groupName + " 群组创建成功");

                        ChatListItemVO chatListItemVO = new ChatListItemVO();
                        chatListItemVO.id = groupVO.id.ToString();
                        chatListItemVO.name = groupVO.groupName;
                        chatListItemVO.type = "_GroupChatPage";
                        chatListItemVO.headImg = "http://pic.qiantucdn.com/58pic/13/81/87/16F58PICUjV_1024.png!/format/webp";
                        Grid g = RenderUtil.generateChatListItemTemp(chatListItemVO);

                        ChatListPage chatListPage = (ChatListPage)guic.getGui("ChatListPage");
                        chatListPage.Dispatcher.Invoke(() =>
                        {
                            chatListPage.listWrapper.Children.Add(g);
                        });
                    });
                }
            }
        }

        public void showGroupUsers(GroupVO groupVO)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                UsersWindow usersWindow = new UsersWindow();
                usersWindow.Dispatcher.Invoke(() =>
                {
                    foreach (UserVO userVO in groupVO.userVOs)
                    {
                        userVO.nickName += userVO.isOnline == 1 ? "(在线)" : "(离线)";
                        usersWindow.usersWrapper.Children.Add(RenderUtil.generateUserTemp(userVO,"hidden"));
                    }
                    usersWindow.ShowDialog();
                });
            });
        }

        public void showGroups(GroupListVO groupListVO)
        {
            GuiCache guic = GuiCache.Instance;
            MyGroupsPage page = (MyGroupsPage)guic.getGui("MyGroupsPage");
            page.Dispatcher.Invoke(() =>
            {
                page.friendsWrapper.Children.Clear();
                List<GroupVO> groupVOs = groupListVO.groupVOs;
               
                for (int i = 0; i < groupVOs.Count; i++)
                {
                    page.friendsWrapper.Children.Add(RenderUtil.generateGroupTemp(groupVOs[i], i));
                }
            });
        }

        public void showFriendsForGroup(FriendsVO friendsVO)
        {
            GuiCache guic = GuiCache.Instance;
            CreateGroupWindow createGroupWindow = (CreateGroupWindow)guic.getGui("CreateGroupWindow");
            createGroupWindow.Dispatcher.Invoke(() =>
            {
                foreach (UserVO userVO in friendsVO.userVOs)
                {
                    userVO.nickName += userVO.isOnline == 1 ? "(在线)" : "(离线)";
                    createGroupWindow.usersWrapper.Children.Add(RenderUtil.generateUserForGroupTemp(userVO));
                }
            });
            
        }
    }
}
